# 同步
# import requests
#
#
# url = 'https://api.web.360kan.com/v1/rank?cat=1'
#
# headers = {
#     'cookie':'__guid=223794332.3003255085760550400.1762222414570.2314; ___sid=223794332.1579397078313448700.1762222414598.4502; __huid=11G%2FCSbB8O%2Badt0aw1mNl2wqxpWjE%2FaktHhaGPz%2FRfFXg%3D; refer_scene=47005; monitor_count=3; __DC_gid=223794332.81786999.1762222414597.1762222720492.31',
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
# }
#
# response = requests.get(url)
# print(response.status_code)
#
# print(response.text)
# # print(response.json())


# 异步协程 (网址是io密集型)
# import aiohttp
# import asyncio
# headers = {
#     'cookie':'__guid=223794332.3003255085760550400.1762222414570.2314; ___sid=223794332.1579397078313448700.1762222414598.4502; __huid=11G%2FCSbB8O%2Badt0aw1mNl2wqxpWjE%2FaktHhaGPz%2FRfFXg%3D; refer_scene=47005; monitor_count=3; __DC_gid=223794332.81786999.1762222414597.1762222720492.31',
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
# }
#
# async def fetch(session, url):
#     async with session.get(url, headers=headers) as response:
#         response =await response.json()
#         print(response)
#         return response
#
#
# async def main():
#     async with aiohttp.ClientSession() as session:
#
#         urls = [f'https://api.web.360kan.com/v1/rank?cat={i}' for i in range(1,7)]
#         tasks = [fetch(session, url) for url in urls]
#         await asyncio.gather(*tasks)
#
# asyncio.run(main())


# 异步线程
import threading
import time
import requests

def t_main(url):
    time.sleep(1)
    response = requests.get(url)
    print(f"线程{response.url}",response.json())

def main():
    print('主线程开始执行')
    urls = [f'https://api.web.360kan.com/v1/rank?cat={i}' for i in range(1,7)]
    threads = []
    for url in urls:
        t = threading.Thread(target=t_main, args=(url,), name=f'threading-{url}')
        threads.append(t)
        t.start()

    for t in threads:
        t.join()
    print('主线程结束')

if __name__ == '__main__':
    main()
